/**
 * 
 */
package com.ai.databao.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ai.databao.common.util.StringUtil;
import net.sf.json.JSONArray;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.ai.databao.utils.AlipayCore;
import com.ai.databao.web.controller.vo.QueryUserInfoVo;

import net.sf.json.JSONObject;

/**
 * @author microzhu
 *
 */
@Service
public class QueryUserInfoSVImpl implements IQueryUserInfoSV {
	private static final Logger log = Logger.getLogger(QueryUserInfoSVImpl.class);
	/* (non-Javadoc)
	 * @see com.ai.databao.service.impl.IQueryUserInfoSV#queryUserCredit(com.ai.databao.web.controller.vo.QueryUserInfoVo)
	 */
	@Override
	public List<JSONObject> queryUserCredit(QueryUserInfoVo queryUserInfoVo) throws Exception {
		// TODO Auto-generated method stub
		String[] methods  = queryUserInfoVo.getQueryMethod().split(",");
		//JSONObject ret = new JSONObject();
		List<JSONObject> list = new ArrayList<JSONObject>();
		for(String method :methods){
			callDataBao(method,queryUserInfoVo,list);
		}
		return list;
	}
	private void callDataBao(String method,QueryUserInfoVo queryUserInfoVo,List<JSONObject> list) throws Exception {
		JSONObject ret = new JSONObject();
		String url = "";
		String key = "";
		String params = "";
		String successCode = "10001";
		String failCode = "10000";
		JSONObject queryInfo = new JSONObject();
		JSONObject data = new JSONObject();
		String code = "";
		switch(method){
			case "checkbox01"://不良信息查询 
				ret.put("total", "30");
				ret.put("name", "不良信息验证");
				url = "http://api.chinadatapay.com/government/safety/1673";
				key = "75334e9a46df2c66b932ca23cba4fb5b";
				params=key+"&name="+queryUserInfoVo.getName()+"&idCard="+queryUserInfoVo.getIdCard();
				queryInfo = postDataToDataBao(url,params);
				code = (String)queryInfo.get("code");
				if(code.equals(successCode)){
					ret.put("value", "30");
				}else if(code.equals(failCode)){
					ret.put("value", "0");
				}else{
					ret.put("value", "0");
					//ret.put("msg", queryInfo.toString());
				}
				ret.put("msg", queryInfo.toString());
				list.add(ret);
				break;
			case "checkbox02"://银行卡三要素信息验证
				url = "https://www.chinadatapay.com/interfaceInfo/interfaceDetails/1449";
				ret.put("value", "15");
				ret.put("total", "15");
				ret.put("name", "银行卡三要素");
				list.add(ret);
				break;	
			case "checkbox03"://全网手机三要素
				//ret.put("value", "0");
				ret.put("total", "15");
				ret.put("name", "全网手机三要素");
				url = "http://api.chinadatapay.com/communication/personal/1708";
				key = "a50ed218d7ae9ca2519121b4bb23b67c";
				params=key+"&personName="+queryUserInfoVo.getName()+"&idNumber="+queryUserInfoVo.getIdCard()+"&mobileNo="+queryUserInfoVo.getPhoneNo();
				queryInfo = postDataToDataBao(url,params);
				code = (String)queryInfo.get("code");
				if(code.equals("10000")){
					//查看验证是不是一致
					data = (JSONObject) queryInfo.get("data");
					String telCom = data.getString("nameCheckResult");
					String cmcMobile = data.getString("idNamePhoneCmccCheck");
					String uniCom = data.getString("idNamePhoneUnicomCheck");
					if(telCom.equals("1")||cmcMobile.equals("1")||uniCom.equals("1")){//如果有一个通过就说明符合要求
						ret.put("value", "15");
					}else{
						ret.put("value", "0");
					}
					
				}else if(code.equals(failCode)){
					ret.put("value", "0");
				}else{
					ret.put("value", "0");
				}
				ret.put("msg", queryInfo.toString());
				list.add(ret);
				break;
			case "checkbox04"://手机号反套利
				//ret.put("value", "0");
				ret.put("total", "15");
				ret.put("name", "手机号反套利");
				url = "http://api.chinadatapay.com/communication/message/1716";
				key = "cfa1cc81043b9776a726f69ba6b7bb5b";
				params=key+"&mobileNo="+queryUserInfoVo.getPhoneNo();
				queryInfo = postDataToDataBao(url,params);
				code = (String)queryInfo.get("code");
				if(code.equals("10000")){
					ret.put("value", "15");
				}else if(code.equals(failCode)){
					ret.put("value", "0");
				}
				ret.put("msg", queryInfo.toString());
				list.add(ret);
				break;
			case "checkbox05"://消费数据
				ret.put("value", "15");
				ret.put("total", "15");
				ret.put("name", "消费数据");
				list.add(ret);
				break;
			case "checkbox06"://手机号在网时长
				//ret.put("value", "5");
				ret.put("total", "10");
				ret.put("name", "手机号在网时长");
				url = "http://api.chinadatapay.com/communication/message/1714";
				key = "6972bbe369eb2e2149754cff7548a685";
				params=key+"&personName="+queryUserInfoVo.getName()+"&idNumber="+queryUserInfoVo.getIdCard()+"&mobileNo="+queryUserInfoVo.getPhoneNo();
				queryInfo = postDataToDataBao(url,params);
				code = (String)queryInfo.get("code");
				if(code.equals("10000")){
					data = (JSONObject) queryInfo.get("data");
					String inTime = data.getString("inTime");
					int time = Integer.parseInt(inTime);
					//0：0, 1：(0,3], 2：(3,6], 3：(6,9], 4：(9,12], 5：(12,24], 6：(24,36], 7：(36,60], 8：60
					if(time>5){
						ret.put("value", "10");//大于24个月
					}else if(time==5){
						ret.put("value", "8");//大于12个月,小于24个月
					}else if(time==3||time==4){//6-12个月
						ret.put("value", "5");
					}else {
						ret.put("value", "0");
					}
					
				}else if(code.equals(failCode)){
					ret.put("value", "0");
				}
				ret.put("msg", queryInfo.toString());
				
				list.add(ret);
				break;
		}
	}
	private JSONObject postDataToDataBao(String url, String params) throws Exception {
		// TODO Auto-generated method stub
		log.info("-----url-----"+url+"========params======="+params);
		String ret = "";
		ret = AlipayCore.HttpUrl_Post_Request(url, params);
		JSONObject json = AlipayCore.parseString2Json(ret);
		log.info("-------查询返回信息---------"+json);
		return json;
	}

	public JSONObject qryIdcardAndName(QueryUserInfoVo queryUserInfoVo) throws Exception{
		JSONObject retObj = new JSONObject();
		retObj.put("retFlag","false");
		retObj.put("retMsg","查询无记录");

		if(StringUtil.isBlank(queryUserInfoVo.getName())){
			retObj.put("retFlag","false");
			retObj.put("retMsg","传入参数[姓名]为空！");
			return retObj;
		}

		if(StringUtil.isBlank(queryUserInfoVo.getIdCard())){
			retObj.put("retFlag","false");
			retObj.put("retMsg","传入参数[身份证号码]为空！");
			return retObj;
		}

		try{
			String url = "http://api.chinadatapay.com/communication/personal/1877";
			String key = "key=a990dde81b9cf9196c2110ad6f9a829a";
			String params=key+"&name="+queryUserInfoVo.getName()+"&idcard="+queryUserInfoVo.getIdCard();
			JSONObject jsObj = postDataToDataBao(url,params);
			String code = (String)jsObj.get("code");
			if(code.equals("10000")){
				JSONObject data = new JSONObject();
				data = (JSONObject) jsObj.get("data");
				String retMsg = data.getString("policeIdAuthentication");
				if("0".equals(retMsg)){
					retObj.put("retMsg","验证一致");
				}else{
					retObj.put("retMsg","验证不一致");
				}
				retObj.put("retFlag","success");
			}
		}catch(Exception e){
			log.error(e.toString());
			retObj.put("retFlag","false");
			retObj.put("retMsg","接口调用异常，请联系管理员");
		}

		return retObj;
	}

	public JSONObject qryBankCardAndName(QueryUserInfoVo queryUserInfoVo) throws Exception{
		JSONObject retObj = new JSONObject();
		retObj.put("retFlag","false");
		retObj.put("retMsg","查询无记录");

		if(StringUtil.isBlank(queryUserInfoVo.getName())){
			retObj.put("retFlag","false");
			retObj.put("retMsg","传入参数[姓名]为空！");
			return retObj;
		}

		if(StringUtil.isBlank(queryUserInfoVo.getIdCard())){
			retObj.put("retFlag","false");
			retObj.put("retMsg","传入参数[身份证号码]为空！");
			return retObj;
		}

		if(StringUtil.isBlank(queryUserInfoVo.getBankNo())){
			retObj.put("retFlag","false");
			retObj.put("retMsg","传入参数[银行卡号]为空！");
			return retObj;
		}

		if(StringUtil.isBlank(queryUserInfoVo.getPhoneNo())){
			retObj.put("retFlag","false");
			retObj.put("retMsg","传入参数[银行卡预留手机号码]为空！");
			return retObj;
		}

		try{
			String url = "http://api.chinadatapay.com/financial/bank/1647";
			String key = "key=88ddd3a502fcb1a8f9dfb3a6fd301d92";
			String params=key+"&name="+queryUserInfoVo.getName()+"&idCard="+queryUserInfoVo.getIdCard()+"&accountNo="+queryUserInfoVo.getBankNo()+"&mobile="+queryUserInfoVo.getPhoneNo();
			JSONObject jsObj = postDataToDataBao(url,params);
			String code = (String)jsObj.get("code");
			if(code.equals("10000")){
				JSONObject data = new JSONObject();
				data = (JSONObject) jsObj.get("data");
				String retMsg = data.getString("status");
				if("0".equals(retMsg)){
					retObj.put("retMsg","验证一致");
				}else{
					retObj.put("retMsg","验证不一致");
				}
				retObj.put("retFlag","success");
			}else{
                JSONObject data = new JSONObject();
                data = (JSONObject) jsObj.get("data");
                String message = data.getString("message");
                retObj.put("retMsg",message);
            }
		}catch(Exception e){
			log.error(e.toString());
			retObj.put("retFlag","false");
			retObj.put("retMsg","接口调用异常，请联系管理员");
		}

		return retObj;
	}

	public JSONObject qryAllNetPhoneInfo(QueryUserInfoVo queryUserInfoVo) throws Exception{
		JSONObject retObj = new JSONObject();
		retObj.put("retFlag","false");
		retObj.put("retMsg","查询无记录");

		if(StringUtil.isBlank(queryUserInfoVo.getName())){
			retObj.put("retFlag","false");
			retObj.put("retMsg","传入参数[姓名]为空！");
			return retObj;
		}

		if(StringUtil.isBlank(queryUserInfoVo.getIdCard())){
			retObj.put("retFlag","false");
			retObj.put("retMsg","传入参数[身份证号码]为空！");
			return retObj;
		}

		if(StringUtil.isBlank(queryUserInfoVo.getPhoneNo())){
			retObj.put("retFlag","false");
			retObj.put("retMsg","传入参数[手机号码]为空！");
			return retObj;
		}

		try{
			String url = "http://api.chinadatapay.com/communication/personal/1708";
			String key = "key=399bc98a201ec4e06448fd9d49e01fc7";
			String params=key+"&personName="+queryUserInfoVo.getName()+"&idNumber="+queryUserInfoVo.getIdCard()+"&mobileNo="+queryUserInfoVo.getPhoneNo();
			JSONObject jsObj = postDataToDataBao(url,params);
			String code = (String)jsObj.get("code");
			if(code.equals("10000")){
				JSONObject data = new JSONObject();
				data = (JSONObject) jsObj.get("data");

				String telCom = data.getString("nameCheckResult");
				String cmcMobile = data.getString("idNamePhoneCmccCheck");
				String uniCom = data.getString("idNamePhoneUnicomCheck");

				retObj.put("retFlag","success");
				if("0".equals(telCom) || "1".equals(cmcMobile) || "1".equals(uniCom)){
					retObj.put("retMsg","验证一致");
				}else{
					retObj.put("retMsg","验证不一致");
				}
			}
		}catch(Exception e){
			log.error(e.toString());
			retObj.put("retFlag","false");
			retObj.put("retMsg","接口调用异常，请联系管理员");
		}

		return retObj;
	}

	public JSONObject qryOrgInfo(QueryUserInfoVo queryUserInfoVo) throws Exception{
		JSONObject retObj = new JSONObject();
		retObj.put("retFlag","false");
		retObj.put("retMsg","查询无记录");

		if(StringUtil.isBlank(queryUserInfoVo.getName())){
			retObj.put("retFlag","false");
			retObj.put("retMsg","传入参数[企业全名]为空！");
			return retObj;
		}

		try{
			String url = "http://api.chinadatapay.com/government/economic/1831";
			String key = "key=fecf9198659ef42e96b691d58cdcee40";
			String params=key+"&name="+queryUserInfoVo.getName();
			JSONObject jsObj = postDataToDataBao(url,params);
			String code = (String)jsObj.get("code");
			if(code.equals("10000")){
				JSONObject data = new JSONObject();
				data = (JSONObject) jsObj.get("data");
                String reg_no = data.getString("reg_no");
				String ename = data.getString("ename");
				String type = data.getString("type");
				String eid = data.getString("eid");
				String org_no = data.getString("org_no");
				String address = data.getString("address");
				String department = data.getString("department");
				String last_updated_time = data.getString("last_updated_time");
				String end_date = data.getString("end_date");
				String start_date = data.getString("start_date");

				String retMsg = "注册号:"+reg_no+"</br>机构名称:"+ename +"</br>类型:"+type +"</br>企业id:"+eid+"</br>组织结构代码:"+org_no+"</br>机构地址:"+address+"</br>部门:"+department+"</br>数据更新时间:"+last_updated_time+"</br>结束日期:"+end_date+"</br>成立日期:"+start_date;
				retObj.put("retFlag","success");
				retObj.put("retMsg",retMsg);
			}
		}catch(Exception e){
			log.error(e.toString());
			retObj.put("retFlag","false");
			retObj.put("retMsg","接口调用异常，请联系管理员");
		}

		return retObj;
	}

	//企业资质证书信息查询接口
	public JSONObject qryOrgQualifications(QueryUserInfoVo queryUserInfoVo) throws Exception{
		JSONObject retObj = new JSONObject();
		retObj.put("retFlag","false");
		retObj.put("retMsg","查询无记录");

		if(StringUtil.isBlank(queryUserInfoVo.getName())){
			retObj.put("retFlag","false");
			retObj.put("retMsg","传入参数[企业全名]为空！");
			return retObj;
		}

		try{
			String url = "http://api.chinadatapay.com/government/economic/1840";
			String key = "key=bc8cfbc201f1337818074836367ae757";
			String params=key+"&name="+queryUserInfoVo.getName()+"&skip=0";
			JSONObject jsObj = postDataToDataBao(url,params);
			String code = (String)jsObj.get("code");
			if(code.equals("10000")){
				String retMsg = "";
				JSONObject data = new JSONObject();
				data = (JSONObject) jsObj.get("data");
				JSONArray items = data.getJSONArray("items");
				if(null != items && items.size() > 0){
					for(int i = 0; i < items.size(); i++){
						JSONObject tmpObject = items.getJSONObject(i);
						String status = tmpObject.getString("code");
						String issue_date = tmpObject.getString("issue_date");
						String num = tmpObject.getString("num");
						String type = tmpObject.getString("type");
						String validity_end = tmpObject.getString("validity_end");
						String remarks = tmpObject.getString("remarks");

						retMsg = retMsg + "第"+i+"条记录信息如下:</br>"+"状态:"+status+"</br>发证时间:"+issue_date+"</br>证书编号:"+num+"</br>资质证书类型:"+type+"</br>截止日期:"+validity_end+"</br>备注:"+remarks;
						if(i<items.size()-1){
							retMsg=retMsg+"</br>";
						}
					}
				}

				retObj.put("retFlag","success");
				retObj.put("retMsg",retMsg);
			}
		}catch(Exception e){
			log.error(e.toString());
			retObj.put("retFlag","false");
			retObj.put("retMsg","接口调用异常，请联系管理员");
		}

		return retObj;
	}

	//企业工商行政处罚信息查询接口
	public JSONObject qryOrgPenalty(QueryUserInfoVo queryUserInfoVo) throws Exception{
		JSONObject retObj = new JSONObject();
		retObj.put("retFlag","false");
		retObj.put("retMsg","查询无记录");

		if(StringUtil.isBlank(queryUserInfoVo.getName())){
			retObj.put("retFlag","false");
			retObj.put("retMsg","传入参数[企业全名]为空！");
			return retObj;
		}

		try{

		}catch(Exception e){
			log.error(e.toString());
			retObj.put("retFlag","false");
			retObj.put("retMsg","接口调用异常，请联系管理员");
		}

		return retObj;
	}

	//企业欠税信息查询接口
	public JSONObject qryOrgOwingTaxes(QueryUserInfoVo queryUserInfoVo) throws Exception{
		JSONObject retObj = new JSONObject();
		retObj.put("retFlag","false");
		retObj.put("retMsg","查询无记录");

		if(StringUtil.isBlank(queryUserInfoVo.getName())){
			retObj.put("retFlag","false");
			retObj.put("retMsg","传入参数[企业全名]为空！");
			return retObj;
		}

		try{
			String url = "http://api.chinadatapay.com/government/economic/1838";
			String key = "key=6275ddc3a3d19f2a62852cc7b4e1c659";
			String params=key+"&name="+queryUserInfoVo.getName()+"&skip=0";
			JSONObject jsObj = postDataToDataBao(url,params);
			String code = (String)jsObj.get("code");
			if(code.equals("10000")){
				String retMsg = "";
				JSONObject data = new JSONObject();
				data = (JSONObject) jsObj.get("data");
				JSONArray items = data.getJSONArray("items");
				if(null != items && items.size() > 0){
					for(int i = 0; i < items.size(); i++){
						JSONObject tmpObject = items.getJSONObject(i);
						String area = tmpObject.getString("area");
						String taxpayer_type = tmpObject.getString("taxpayer_type");
						String pub_date = tmpObject.getString("pub_date");
						String overdue_amount = tmpObject.getString("overdue_amount");
						String address = tmpObject.getString("address");
						String overdue_type = tmpObject.getString("overdue_type");
						String taxpayer_num = tmpObject.getString("taxpayer_num");
						String oper_name = tmpObject.getString("oper_name");
						String oper_id_num = tmpObject.getString("oper_id_num");
						String pub_department = tmpObject.getString("pub_department");
						String overdue_period = tmpObject.getString("overdue_period");
						String curr_overdue_amount = tmpObject.getString("curr_overdue_amount");

						retMsg = retMsg + "第"+i+"条记录信息如下:</br>"+"所属市县区:"+area+"</br>纳税人类型:"+taxpayer_type
								+"</br>发布日期:"+pub_date+"</br>欠税余额:"+overdue_amount+"</br>经营地点:"+address+"</br>欠税税种:"+overdue_type
								+"</br>纳税人识别号:"+taxpayer_num+"</br>负责人姓名:"+oper_name+"</br>证件号码:"+oper_id_num+"</br>发布单位:"+pub_department
								+"</br>欠税所属期:"+overdue_period+"</br>当前发生的欠税余额:"+curr_overdue_amount;
						if(i<items.size()-1){
							retMsg=retMsg+"</br>";
						}
					}
				}

				retObj.put("retFlag","success");
				retObj.put("retMsg",retMsg);
			}
		}catch(Exception e){
			log.error(e.toString());
			retObj.put("retFlag","false");
			retObj.put("retMsg","接口调用异常，请联系管理员");
		}

		return retObj;
	}

	//企业经营异常查询接口
	public JSONObject qryOrgAbnormal(QueryUserInfoVo queryUserInfoVo) throws Exception{
		JSONObject retObj = new JSONObject();
		retObj.put("retFlag","false");
		retObj.put("retMsg","查询无记录");

		if(StringUtil.isBlank(queryUserInfoVo.getName())){
			retObj.put("retFlag","false");
			retObj.put("retMsg","传入参数[企业全名]为空！");
			return retObj;
		}

		try{
			String url = "http://api.chinadatapay.com/government/education/1836";
			String key = "key=b5312e2bf51510724e4b50deb062ea91";
			String params=key+"&name="+queryUserInfoVo.getName()+"&skip=0";
			JSONObject jsObj = postDataToDataBao(url,params);
			String code = (String)jsObj.get("code");
			if(code.equals("10000")){
				String retMsg = "";
				JSONObject data = new JSONObject();
				data = (JSONObject) jsObj.get("data");
				JSONArray items = data.getJSONArray("items");
				if(null != items && items.size() > 0){
					for(int i = 0; i < items.size(); i++){
						JSONObject tmpObject = items.getJSONObject(i);
						String province = tmpObject.getString("province");
						String reg_no = tmpObject.getString("reg_no");
						String in_date = tmpObject.getString("in_date");
						String in_reason = tmpObject.getString("in_reason");
						String out_date = tmpObject.getString("out_date");
						String name = tmpObject.getString("name");
						String out_reason = tmpObject.getString("out_reason");
						String department = tmpObject.getString("department");

						retMsg = retMsg + "第"+i+"条记录信息如下:</br>"+"省份:"+province+"</br>注册号:"+reg_no
								+"</br>列入时间:"+in_date+"</br>列入原因:"+in_reason+"</br>移出时间:"+out_date+"</br>移出原因:"+out_reason
								+"</br>公司名:"+name+"</br>做出决定机关:"+department;
						if(i<items.size()-1){
							retMsg=retMsg+"</br>";
						}
					}
				}

				retObj.put("retFlag","success");
				retObj.put("retMsg",retMsg);
			}
		}catch(Exception e){
			log.error(e.toString());
			retObj.put("retFlag","false");
			retObj.put("retMsg","接口调用异常，请联系管理员");
		}

		return retObj;
	}

	//企业族谱查询接口
	public JSONObject qryOrgGenealogy(QueryUserInfoVo queryUserInfoVo) throws Exception{
		JSONObject retObj = new JSONObject();
		retObj.put("retFlag","false");
		retObj.put("retMsg","查询无记录");

		if(StringUtil.isBlank(queryUserInfoVo.getName())){
			retObj.put("retFlag","false");
			retObj.put("retMsg","传入参数[企业全名]为空！");
			return retObj;
		}

		try{

		}catch(Exception e){
			log.error(e.toString());
			retObj.put("retFlag","false");
			retObj.put("retMsg","接口调用异常，请联系管理员");
		}

		return retObj;
	}

	//法院判决信息查询接口
	public JSONObject qryJudgment(QueryUserInfoVo queryUserInfoVo) throws Exception{
		JSONObject retObj = new JSONObject();
		retObj.put("retFlag","false");
		retObj.put("retMsg","查询无记录");

		if(StringUtil.isBlank(queryUserInfoVo.getName())){
			retObj.put("retFlag","false");
			retObj.put("retMsg","传入参数[企业全名]为空！");
			return retObj;
		}

		try{
			String url = "http://api.chinadatapay.com/government/safety/1824";
			String key = "key=2802bd54466ea731c1f3e471b44a0ceb";
			String params=key+"&name="+queryUserInfoVo.getName()+"&skip=0";
			JSONObject jsObj = postDataToDataBao(url,params);
			String code = (String)jsObj.get("code");
			if(code.equals("10000")){
				String retMsg = "";
				JSONObject data = new JSONObject();
				data = (JSONObject) jsObj.get("data");
				JSONArray items = data.getJSONArray("items");
				if(null != items && items.size() > 0){
					for(int i = 0; i < items.size(); i++){
						JSONObject tmpObject = items.getJSONObject(i);
						String date = tmpObject.getString("date");
						String id = tmpObject.getString("id");
						String title = tmpObject.getString("title");
						String type = tmpObject.getString("type");

						retMsg = retMsg + "第"+i+"条记录信息如下:</br>"+"提交日期:"+date+"</br>法院判决id:"+id
								+"</br>标题:"+title+"</br>类型:"+type;
						if(i<items.size()-1){
							retMsg=retMsg+"</br>";
						}
					}
				}

				retObj.put("retFlag","success");
				retObj.put("retMsg",retMsg);
			}
		}catch(Exception e){
			log.error(e.toString());
			retObj.put("retFlag","false");
			retObj.put("retMsg","接口调用异常，请联系管理员");
		}

		return retObj;
	}
}
